package com.ccx.library.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.springframework.http.HttpStatus;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class R<T> {

    private Integer code;
    private String message;
    private T data;
//  200
    public static<T> R<T> SUCCESS(T data){
        R r = new R<>();
        r.setCode(HttpStatus.OK.value());
        r.setMessage(HttpStatus.OK.getReasonPhrase());
        r.setData(data);
        return r;
    }

    public static<T> R<T> SUCCESS(){
        R r = new R<>();
        DataResponse dataResponse=new SimpleDataResponse();
        r.setCode(HttpStatus.OK.value());
        r.setMessage(HttpStatus.OK.getReasonPhrase());
        r.setData(dataResponse.getTime());
        return r;
    }

    public static <T> R<T> FAIL(T error){
        R r = new R<>();
        DataResponse dataResponse=new SimpleDataResponse();
        r.setCode(HttpStatus.NOT_ACCEPTABLE.value());
        r.setMessage(HttpStatus.NOT_ACCEPTABLE.getReasonPhrase());
        r.setData(error);
        return r;
    }
//406
    public static <T> R<T> FAIL(){
        R r = new R<>();
        DataResponse dataResponse=new SimpleDataResponse();
        r.setCode(HttpStatus.NOT_ACCEPTABLE.value());
        r.setMessage(HttpStatus.NOT_ACCEPTABLE.getReasonPhrase());
        r.setData(dataResponse.getTime());
        return r;
    }
// 401
    public static <T> R<T> NotToken(){
        R r = new R<>();
        DataResponse dataResponse=new SimpleDataResponse();
        r.setCode(HttpStatus.UNAUTHORIZED.value());
        r.setMessage(HttpStatus.UNAUTHORIZED.getReasonPhrase());
        r.setData(dataResponse.getTime());
        return r;
    }
//  413
    public static <T> R<T> LOGIN_FAIL(){
        R r=new R();
        DataResponse dataResponse=new SimpleDataResponse();
        r.setCode(HttpStatus.PAYLOAD_TOO_LARGE.value());
        r.setMessage("账号或密码错误");
        r.setData(dataResponse.getTime());
        return r;
    }
//    420
    public static <T> R<T> LOGIN_STATUS(){
        R r=new R();
        DataResponse dataResponse=new SimpleDataResponse();
        r.setCode(HttpStatus.METHOD_FAILURE.value());
        r.setMessage("账号已冻结");
        r.setData(dataResponse.getTime());
        return r;
    }

    public static <T> R<T> AccessDenied(){
        R r = new R<>();
        DataResponse dataResponse=new SimpleDataResponse();
        r.setCode(777);
        r.setMessage("权限不足");
        r.setData(dataResponse.getTime());
        return r;
    }
}
